package newfangle.demos

import java.awt.Color._
import java.util.Calendar

import org.pwwong.fxscalautils.{FXScalaApp,DisplayMode}
import org.pwwong.fxscalautils.Predef._
import org.pwwong.fxscalautils.components.controls.Rectangle

import org.pwwong.scalautils.animation.Predef._
import org.pwwong.scalautils.signals._
import org.pwwong.scalautils.signals.Predef._
import org.pwwong.scalautils.time.Predef._

object ClockDemo extends FXScalaApp(1024,768,DisplayMode.Windowed) {
  def cal(i:Int) = Calendar.getInstance.get(i)
  
  // Calculate angles for the Hour, Minute, and Second Hands 
  def curSecondAngle = 2.0*Math.Pi/60.0 * ( cal(Calendar.MILLISECOND).toDouble/1000.0 + cal(Calendar.SECOND) )  
  def curMinuteAngle = 2.0*Math.Pi/60.0 * cal(Calendar.MINUTE) + curSecondAngle/60.0 
  def curHourAngle =   2.0*Math.Pi/12.0 * cal(Calendar.HOUR)   + curMinuteAngle/60.0
  
  // Create the Hour, Minute, and Second hands
  // The anchor point, 10% from the bottom of the rectangle, is placed at the center of the screen
  val hourHand   = Rectangle() anchor(Center,Bottom(-10 %%)) x(Center) y(Middle) w(25 px) h(20 %%) fillPaint(red)
  val minuteHand = Rectangle() anchor(Center,Bottom(-10 %%)) x(Center) y(Middle) w(15 px) h(45 %%) fillPaint(green)
  val secondHand = Rectangle() anchor(Center,Bottom(-10 %%)) x(Center) y(Middle) w( 5 px) h(45 %%) fillPaint(blue) 
  
  // Layout clock hands onto the canvas
  contents(
    hourHand   rotation( eval{ curHourAngle } every(16 ms) ),
    minuteHand rotation( eval{ curMinuteAngle } every(16 ms) ),
    secondHand rotation( eval{ curSecondAngle } every(16 ms) )
  )
}